package com.ssh.cp.service;

import java.io.File;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import org.springframework.stereotype.Service;

import com.ssh.cp.entity.FileEntity;
import com.ssh.cp.idao.IFileClassDao;
import com.ssh.cp.idao.IFileDao;
import com.ssh.cp.iservice.IFileService;
import com.ssh.cp.util.FileUtil;


@Service
public class FileService implements IFileService{

	@Resource
	private IFileDao fileDao;
	
	
	
	@Resource
	private IFileClassDao fileClassDao;
	
	private Timestamp nowDate;


	public String doFileSave(File uploadFlie,FileEntity fileEntity) {
		nowDate=new Timestamp(new Date().getTime());
		fileEntity.setUploadtime(nowDate);
		fileDao.saveFile(fileEntity);
		FileUtil.uploadFile(uploadFlie, fileEntity.getSavepath(),fileEntity.getFilename());
		return "success";
	}

	public List<?> listFile(int recordsTotal,int recordsFiltered){
		
		return fileDao.listAllFile(recordsTotal,recordsFiltered);
	}
	
	public List<?> listFileByFileName(String search,int recordsTotal,int recordsFiltered){
		
		return fileDao.listFileByFileName(search,recordsTotal,recordsFiltered);
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return fileDao.getFileNum();
	}
	
	
	@Override
	public List<?> listFileByUserId(int userid, int start, int length) {
		// TODO Auto-generated method stub
		return fileDao.listFileByUserId(userid, start, length);
	}

	@Override
	public List<?> listFileClass() {
		// TODO Auto-generated method stub
		
		return fileClassDao.listFileClass();
	}

	@Override
	public int getCount(int userid) {
		// TODO Auto-generated method stub
		return fileDao.getFileNum(userid);
	}

	@Override
	public List<?> listFileByFileName(int userid, String search, int recordsTotal, int recordsFiltered) {
		// TODO Auto-generated method stub
		return fileDao.listFileByFileName(userid, search, recordsTotal, recordsFiltered);
	}

	@Override
	public List<?> listFileByLibrary(int userid, int libraryid, int start, int length) {
		// TODO Auto-generated method stub
		return fileDao.listFileByLibrary(userid,libraryid,start,length);
	}

	@Override
	public List<?> listFileByClass(int userid, int classid, int start, int length) {
		// TODO Auto-generated method stub
		return fileDao.listFileByUserAndClass(userid, classid, start, length);
	}

	@Override
	
	public boolean doFileUpdate(String oldfile, String newfile, FileEntity updateEntity) {
		// TODO Auto-generated method stub
		File of=new File(oldfile);
		File nf=new File(newfile);
		if(!of.getName().equals(nf.getName())) {
			updateEntity.setSavepath(newfile);
			fileDao.updateFile(updateEntity);
			if(!nf.exists()) {			
				of.renameTo(nf);				
				return true;
			}else {
				return false;
			}
			
		}else {
			return false;
		}
		
		}

	@Override
	public File downloadAll(String userPath) {
		// TODO Auto-generated method stub
		//File folder=new File(userPath);
		String pzip=".zip";
		String targetZip=userPath+pzip;
		System.out.println("=========targetZip:  "+targetZip);
		FileUtil.zipMultiFile(userPath+File.separator, targetZip);
		File zipUserFile=new File(targetZip);
		if(zipUserFile.exists())
			return zipUserFile;
		else
			return null;
	}

	@Override
	public List<?> listShareFile(int start, int length) {
		// TODO Auto-generated method stub
		return fileDao.listShareFile(start, length);
	}

	@Override
	public int getShareFileNum() {
		// TODO Auto-generated method stub
		return fileDao.getShareFileNum();
	}

	@Override
	public List<?> listShareFile(String search, int start, int length) {
		// TODO Auto-generated method stub
		return fileDao.listShareFile(search,start, length);
	}

	@Override
	public File sharedFileDownload(int fileid) {
		// TODO Auto-generated method stub
		
		String filepath=fileDao.getFilePathById(fileid)+fileDao.checkShareAndDownload(fileid);
		File sfd=new File(filepath);
	
			
			if(sfd.exists()&&sfd.isFile())
				return sfd;
			else		
				return null;
	
	}

	@Override
	public List<?> listAllUserFile(int userid) {
		// TODO Auto-generated method stub
		return fileDao.listAllUserFile(userid);
	}
}

	

